package com.meeting.task;

import com.meeting.entity.Orders;
import com.meeting.mapper.OrdersMapper;
import com.meeting.mapper.RoomsMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Component
@Slf4j
public class OrdersTask {
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private RoomsMapper roomsMapper;

    /**
     * 处理超时订单
     */
    @Scheduled(cron = "0 * * * * *")
    public void processTimeOutOrders(){
        log.info("定时处理超时订单：{}", LocalDateTime.now());
        LocalDateTime time = LocalDateTime.now().plusMinutes(-30);
        List<Orders> orderList = ordersMapper.selectByStatusAndOrderTimeLT(0,time);

        if(orderList != null && !orderList.isEmpty()){
            for (Orders order : orderList){
                order.setStatus(4);
                order.setCancelTime(LocalDateTime.now());
                ordersMapper.updateById(order);
            }
        }
    }

    @Scheduled(cron = "0 0 * * * *")
    public void processFinishOrders(){
        log.info("定时处理已完成订单:{}",LocalDateTime.now());
        LocalDateTime time = LocalDateTime.now();
        List<Orders> orderList = ordersMapper.selectByEndTime(time);
        if(orderList!=null && !orderList.isEmpty()){
            for (Orders order:orderList){
                order.setStatus(3);
                ordersMapper.updateById(order);
            }
        }
    }
}
